Skip to content

虚拟 dom

js
// 用 js 描述的 dom 结构
// tag key children props text

export default function h(tag, props, ...children) {
  let key = props.key;
  delete props.key;
  children = children.map((item) => {
    if (typeof item === "object") {
      return item;
    } else {
      return createElement(undefined, undefined, undefined, undefined, item);
    }
  });
  return createElement(tag, props, key, children);
}
function createElement(tag, props, key, children, text) {
  return {
    tag,
    key,
    props,
    children,
    text,
  };
}

在 MIT 许可下发布